package com.youway.ddd.demo1.jpa.bookstore.service;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youway.ddd.demo1.domain.bookstore.Book;
import com.youway.ddd.demo1.domain.bookstore.Bookshelf;
import com.youway.ddd.demo1.domain.bookstore.Bookstore;
import com.youway.ddd.demo1.jpa.bookstore.entity.BookEntity2;
import com.youway.ddd.demo1.jpa.bookstore.entity.BookshelfEntity2;
import com.youway.ddd.demo1.jpa.bookstore.repository.BookRepository2;
import com.youway.ddd.demo1.jpa.bookstore.repository.BookshelfRepository2;

@Service
public class BookstoreService2 implements Bookstore {

    @Autowired BookshelfRepository2 bookshelfRepository2;
    @Autowired BookRepository2 bookRepository2;
    
    @Override
    public Book register(String title, double price) {
        BookEntity2 book = new BookEntity2();
        book.setTitle(title);
        book.setPrice(price);
        return bookRepository2.save(book);
    }
    
    @Override
    public Bookshelf addBookshelf(String category) {
        BookshelfEntity2 entity = new BookshelfEntity2();
        entity.setCategory(category);
        return bookshelfRepository2.save(entity);
    }

    @Override
    public Optional<Bookshelf> findShelf(String category) {
        return Optional.ofNullable(bookshelfRepository2.findByCategory(category));
    }
}